package com.ukao.pad.utils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by ukaodev on 2017/6/2.
 */

public class MyDateUtils {



    private static final SimpleDateFormat DATE_FORMAT_DATETIME = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat DATE_FORMAT_DATETIMES = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    private static final SimpleDateFormat DATE_FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd");

    private static final SimpleDateFormat DATE_FORMAT_TIME = new SimpleDateFormat("HH:mm:ss");




    /**
     * 格式化日期时间  年月日时分秒
     *
     * @param date
     * @return
     */
    public static String formatDataTime(long date) {
            if (date==0){
                return "";
            }
        return DATE_FORMAT_DATETIME.format(new Date(date));
    }

    public static String formatDataTime(Date date) {

        return DATE_FORMAT_DATETIME.format(date);
    }


    public static String formatDataTimes(long date) {
        if (date==0){
            return "";
        }
        return DATE_FORMAT_DATETIMES.format(new Date(date));
    }

    /**
     * 格式化日期  年月日
     *
     * @param date
     * @return
     */
    public static String formatDate(long date) {
        return DATE_FORMAT_DATE.format(new Date(date));
    }


    public static String formatDate(Date date) {
        return DATE_FORMAT_DATE.format(date);
    }




    /**
     * 格式化时间  时分秒
     *
     * @param date
     * @return
     */
    public static String formatTime(long date) {
        return DATE_FORMAT_TIME.format(new Date(date));
    }

    /**
     * 自定义格式的格式化日期时间
     *
     * @param beginDate
     * @param format
     * @return
     */
    public static String formatDateCustom(String beginDate, String format) {
        if (CheckUtils.isEmpty(beginDate))return "";
        return new SimpleDateFormat(format).format(new Date(Long.parseLong(beginDate)));
    }

    public static String formatDateCustom(long beginDate, String format) {
        if (CheckUtils.isNull(beginDate)||beginDate==0){
            return  "";
        }

        return new SimpleDateFormat(format).format(new Date(beginDate));
    }

    public static String formatDateCustom(Date beginDate, String format) {
        if (CheckUtils.isNull(beginDate))return "";
        return new SimpleDateFormat(format).format(beginDate);
    }



    /**
     * 获取系统时间
     *
     * @return
     */
    public static String getTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        return cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND);
    }

    /**
     * 获取系统日期
     *
     * @return
     */
    public static String getDate() {

        return new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis());
    }

    /**
     * 获取系统日期时间
     *
     * @return
     */
    public static String getDateTime() {
        return DATE_FORMAT_DATETIME.format(System.currentTimeMillis());
    }



    public static String getDateTime(String format) {
        return new SimpleDateFormat(format).format(System.currentTimeMillis());
    }


    /**
     *减去25年前
     */
    public static Date getDateYearsago(  ) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, -25);
        return cal.getTime();
    }






    /**
     * 根据date计算年龄
     *
     * @param birthDay
     * @return
     * @throws Exception
     */
    public static int getAge(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth)
                    age--;
            } else {
                age--;
            }
        }
        return age;
    }

    /**
     * 根据date返回String
     * @param date
     * @param formatStr
     * @return
     */
    public static String getTime(Date date,String formatStr) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat(formatStr, Locale.CHINA);
        return format.format(date);
    }
    /**
     * 根据String返回date
     * @param time
     * @param formatStr
     * @return
     */
    public static Date getTime(String time,String formatStr) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(formatStr,Locale.CHINA);
        Date date = format.parse(time);
        return date;
    }

    public static long getTimes(String time,String formatStr) throws ParseException{
        SimpleDateFormat format = new SimpleDateFormat(formatStr,Locale.CHINA);
        Date date = format.parse(time);
        return date.getTime();
    }




    /**
     *
     * @param dateString  1978-5-20
     * @param style
     * @return
     */
    public static Date stringDate(String dateString, String style) {
        String time = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(style);
        Date date = null;
        if (dateString == null || dateString.length() < 6) {
            return null;
        }
        try {
            ParsePosition pos = new ParsePosition(0);
            date = simpleDateFormat.parse(dateString, pos);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return date;
    }





    /**
     * 获取当前时间的前一天时间
     * @param cl
     * @return
     */
    public static Calendar getBeforeDay(Calendar cl){
        int day = cl.get(Calendar.DATE);
        cl.set(Calendar.DATE, day-1);
        return cl;
    }

    /**
     * 获取当前时间的前一月时间
     * @param cl
     * @return
     */
    public static Calendar getBeforeMouth(Calendar cl){
        int day = cl.get(Calendar.MONTH);
        cl.set(Calendar.MONTH, day-1);
        return cl;
    }

    /**
     * 获取当前时间的后一天时间
     * @param cl
     * @return
     */
    public static Calendar getAfterDay(Calendar cl){

        int day = cl.get(Calendar.DATE);
        cl.set(Calendar.DATE, day+1);
        return cl;
    }
    /**
     * 获取当前时间的后一天时间
     * @param cl
     * @return
     */
    public static Calendar getAfterMonth(Calendar cl){

        int day = cl.get(Calendar.MONTH);
        cl.set(Calendar.MONTH, day+1);
        return cl;
    }

}
